/**
 * 
 */
package net.jeanhwea.gold.model;

/**
 * 
 * The main controller in the GoldView project
 * 
 * @author Jeanhwea
 *
 */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.ArrayList;

import net.jeanhwea.gold.bean.Item;


public class DataManager {
	private String databaseURL = "";
	private String databaseUser = "";
	private String databasePasswd = "";
	
	public void setDbURL(String aURL) {
		this.databaseURL = aURL;
	}
	public String getDbURL() {
		return this.databaseURL;
	}

	public void setDbUser(String aUser) {
		this.databaseUser = aUser;
	}
	public String getDbUser() {
		return this.databaseUser;
	}
	
	public void setDbPasswd(String aPasswd) {
		this.databasePasswd = aPasswd;
	}
	public String getDbPasswd() {
		return this.databasePasswd;
	}
	
	/**
	 * get connected to database
	 * 
	 * @author Jeanhwea
	 * @return a connection to the database
	 */
	public Connection getConnection(){
		Connection conn = null;
		try{
			conn = (Connection) DriverManager.getConnection(getDbURL(), getDbUser(), getDbPasswd());
		}catch(SQLException e){
			System.out.println("Could not connect to DB: " + e.getMessage());
		}
		return conn;
	}
	
	/**
	 * close the connection of parameter sConn
	 * 
	 * @author Jeanhwea
	 * @param aConn
	 */
	public void closeConnection(Connection aConn) {
		if(aConn != null){
			try{
				aConn.close();
			}catch(SQLException e){ }
		}
	}
	
	/**
	 * search result in database
	 * 
	 * @param id
	 * @return ArrayList of Item 
	 */
	public ArrayList<Item> getResultByID(int id) {
		return ItemPeer.searchItemById(this, id);
	}
	
	public ArrayList<Item> getResultByPrice(double price) {
		return ItemPeer.searchItemByPrice(this, price);
	}
	
	
}

